package utils

import (
	"context"
	"fmt"
	"order_srv/handler/dao"
	"order_srv/handler/models"
	"time"
)

// LogOperation 记录操作日志
func LogOperation(ctx context.Context, logType, module, operation, resourceType, resourceID, userID, userName, userType, description string) error {
	log := &models.OperationLog{
		LogType:      logType,
		Module:       module,
		Operation:    operation,
		ResourceType: resourceType,
		ResourceID:   resourceID,
		UserID:       userID,
		UserName:     userName,
		UserType:     userType,
		Result:       models.ResultSuccess,
		Description:  description,
		CreatedAt:    time.Now(),
	}

	return dao.CreateLog(ctx, log)
}

// LogError 记录错误日志
func LogError(ctx context.Context, module, operation, resourceType, resourceID, userID, userName, userType, errorMessage, description string) error {
	log := &models.OperationLog{
		LogType:      models.LogTypeError,
		Module:       module,
		Operation:    operation,
		ResourceType: resourceType,
		ResourceID:   resourceID,
		UserID:       userID,
		UserName:     userName,
		UserType:     userType,
		Result:       models.ResultError,
		ErrorMessage: errorMessage,
		Description:  description,
		CreatedAt:    time.Now(),
	}

	return dao.CreateLog(ctx, log)
}

// LogOrderOperation 记录订单操作日志
func LogOrderOperation(ctx context.Context, operation, orderID, operatorID, operatorType, reason string) error {
	return LogOperation(
		ctx,
		models.LogTypeOperation,
		models.ModuleOrder,
		operation,
		models.ResourceTypeOrder,
		orderID,
		operatorID,
		"操作员", // 这里应该从数据库获取真实姓名
		operatorType,
		reason,
	)
}

// LogOrderError 记录订单错误日志
func LogOrderError(ctx context.Context, operation, orderID, operatorID, operatorType, errorMessage, reason string) error {
	return LogError(
		ctx,
		models.ModuleOrder,
		operation,
		models.ResourceTypeOrder,
		orderID,
		operatorID,
		"操作员", // 这里应该从数据库获取真实姓名
		operatorType,
		errorMessage,
		reason,
	)
}

// LogStateTransition 记录状态转换日志
func LogStateTransition(ctx context.Context, orderID, operatorID, operatorType, event, fromState, toState, reason string) error {
	description := fmt.Sprintf("订单状态从 %s 转换为 %s，事件: %s", fromState, toState, event)
	return LogOperation(
		ctx,
		models.LogTypeOperation,
		models.ModuleOrder,
		"transition",
		models.ResourceTypeOrder,
		orderID,
		operatorID,
		"操作员", // 这里应该从数据库获取真实姓名
		operatorType,
		description,
	)
}

// LogUserOperation 记录用户操作日志
func LogUserOperation(ctx context.Context, operation, userID, userName, userType, description string) error {
	return LogOperation(
		ctx,
		models.LogTypeOperation,
		models.ModuleUser,
		operation,
		models.ResourceTypeUser,
		userID,
		userID,
		userName,
		userType,
		description,
	)
}

// LogSystemOperation 记录系统操作日志
func LogSystemOperation(ctx context.Context, operation, description string) error {
	return LogOperation(
		ctx,
		models.LogTypeOperation,
		models.ModuleSystem,
		operation,
		"system",
		"system",
		"system",
		"system",
		"system",
		description,
	)
}

// LogSecurityEvent 记录安全事件日志
func LogSecurityEvent(ctx context.Context, operation, userID, userName, userType, description string) error {
	return LogOperation(
		ctx,
		models.LogTypeSecurity,
		models.ModuleSystem,
		operation,
		models.ResourceTypeUser,
		userID,
		userID,
		userName,
		userType,
		description,
	)
}

// LogAuditEvent 记录审计事件日志
func LogAuditEvent(ctx context.Context, operation, resourceType, resourceID, userID, userName, userType, description string) error {
	return LogOperation(
		ctx,
		models.LogTypeAudit,
		models.ModuleSystem,
		operation,
		resourceType,
		resourceID,
		userID,
		userName,
		userType,
		description,
	)
}
